home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Objects / funcobject.c < prev    next >
C/C++ Source or Header  |  1998-05-30  |  6KB  |  229 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Function object implementation */
  33.  
  34. #include "Python.h"
  35. #include "compile.h"
  36. #include "structmember.h"
  37.  
  38. #include "protos/funcobject_protos.h"
  39.  
  40. PyObject *
  41. PyFunction_New(code, globals)
  42.     PyObject *code;
  43.     PyObject *globals;
  44. {
  45.     PyFunctionObject *op = PyObject_NEW(PyFunctionObject,
  46.                         &PyFunction_Type);
  47.     if (op != NULL) {
  48.         PyObject *doc;
  49.         PyObject *consts;
  50.         Py_INCREF(code);
  51.         op->func_code = code;
  52.         Py_INCREF(globals);
  53.         op->func_globals = globals;
  54.         op->func_name = ((PyCodeObject *)code)->co_name;
  55.         Py_INCREF(op->func_name);
  56.         op->func_defaults = NULL; /* No default arguments */
  57.         consts = ((PyCodeObject *)code)->co_consts;
  58.         if (PyTuple_Size(consts) >= 1) {
  59.             doc = PyTuple_GetItem(consts, 0);
  60.             if (!PyString_Check(doc))
  61.                 doc = Py_None;
  62.         }
  63.         else
  64.             doc = Py_None;
  65.         Py_INCREF(doc);
  66.         op->func_doc = doc;
  67.     }
  68.     return (PyObject *)op;
  69. }
  70.  
  71. PyObject *
  72. PyFunction_GetCode(op)
  73.     PyObject *op;
  74. {
  75.     if (!PyFunction_Check(op)) {
  76.         PyErr_BadInternalCall();
  77.         return NULL;
  78.     }
  79.     return ((PyFunctionObject *) op) -> func_code;
  80. }
  81.  
  82. PyObject *
  83. PyFunction_GetGlobals(op)
  84.     PyObject *op;
  85. {
  86.     if (!PyFunction_Check(op)) {
  87.         PyErr_BadInternalCall();
  88.         return NULL;
  89.     }
  90.     return ((PyFunctionObject *) op) -> func_globals;
  91. }
  92.  
  93. PyObject *
  94. PyFunction_GetDefaults(op)
  95.     PyObject *op;
  96. {
  97.     if (!PyFunction_Check(op)) {
  98.         PyErr_BadInternalCall();
  99.         return NULL;
  100.     }
  101.     return ((PyFunctionObject *) op) -> func_defaults;
  102. }
  103.  
  104. int
  105. PyFunction_SetDefaults(op, defaults)
  106.     PyObject *op;
  107.     PyObject *defaults;
  108. {
  109.     if (!PyFunction_Check(op)) {
  110.         PyErr_BadInternalCall();
  111.         return -1;
  112.     }
  113.     if (defaults == Py_None)
  114.         defaults = NULL;
  115.     else if (PyTuple_Check(defaults)) {
  116.         Py_XINCREF(defaults);
  117.     }
  118.     else {
  119.         PyErr_SetString(PyExc_SystemError, "non-tuple default args");
  120.         return -1;
  121.     }
  122.     Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
  123.     ((PyFunctionObject *) op) -> func_defaults = defaults;
  124.     return 0;
  125. }
  126.  
  127. /* Methods */
  128.  
  129. #define OFF(x) offsetof(PyFunctionObject, x)
  130.  
  131. static struct memberlist func_memberlist[] = {
  132.     {"func_code",    T_OBJECT,    OFF(func_code),        READONLY},
  133.     {"func_globals",T_OBJECT,    OFF(func_globals),    READONLY},
  134.     {"func_name",    T_OBJECT,    OFF(func_name),        READONLY},
  135.     {"__name__",    T_OBJECT,    OFF(func_name),        READONLY},
  136.     {"func_defaults",T_OBJECT,    OFF(func_defaults),    READONLY},
  137.     {"func_doc",    T_OBJECT,    OFF(func_doc)},
  138.     {"__doc__",    T_OBJECT,    OFF(func_doc)},
  139.     {NULL}    /* Sentinel */
  140. };
  141.  
  142. static PyObject *
  143. func_getattr(op, name)
  144.     PyFunctionObject *op;
  145.     char *name;
  146. {
  147.     if (name[0] != '_' && PyEval_GetRestricted()) {
  148.         PyErr_SetString(PyExc_RuntimeError,
  149.           "function attributes not accessible in restricted mode");
  150.         return NULL;
  151.     }
  152.     return PyMember_Get((char *)op, func_memberlist, name);
  153. }
  154.  
  155. static void
  156. func_dealloc(op)
  157.     PyFunctionObject *op;
  158. {
  159.     Py_DECREF(op->func_code);
  160.     Py_DECREF(op->func_globals);
  161.     Py_DECREF(op->func_name);
  162.     Py_XDECREF(op->func_defaults);
  163.     Py_XDECREF(op->func_doc);
  164.     PyMem_DEL(op);
  165. }
  166.  
  167. static PyObject*
  168. func_repr(op)
  169.     PyFunctionObject *op;
  170. {
  171.     char buf[140];
  172.     if (op->func_name == Py_None)
  173.         sprintf(buf, "<anonymous function at %lx>", (long)op);
  174.     else
  175.         sprintf(buf, "<function %.100s at %lx>",
  176.             PyString_AsString(op->func_name),
  177.             (long)op);
  178.     return PyString_FromString(buf);
  179. }
  180.  
  181. static int
  182. func_compare(f, g)
  183.     PyFunctionObject *f, *g;
  184. {
  185.     int c;
  186.     if (f->func_globals != g->func_globals)
  187.         return (f->func_globals < g->func_globals) ? -1 : 1;
  188.     if (f->func_defaults != g->func_defaults) {
  189.         if (f->func_defaults == NULL)
  190.             return -1;
  191.         if (g->func_defaults == NULL)
  192.             return 1;
  193.         c = PyObject_Compare(f->func_defaults, g->func_defaults);
  194.         if (c != 0)
  195.             return c;
  196.     }
  197.     return PyObject_Compare(f->func_code, g->func_code);
  198. }
  199.  
  200. static long
  201. func_hash(f)
  202.     PyFunctionObject *f;
  203. {
  204.     long h;
  205.     h = PyObject_Hash(f->func_code);
  206.     if (h == -1) return h;
  207.     h = h ^ (long)f->func_globals;
  208.     if (h == -1) h = -2;
  209.     return h;
  210. }
  211.  
  212. PyTypeObject PyFunction_Type = {
  213.     PyObject_HEAD_INIT(&PyType_Type)
  214.     0,
  215.     "function",
  216.     sizeof(PyFunctionObject),
  217.     0,
  218.     (destructor)func_dealloc, /*tp_dealloc*/
  219.     0,        /*tp_print*/
  220.     (getattrfunc)func_getattr, /*tp_getattr*/
  221.     0,        /*tp_setattr*/
  222.     (cmpfunc)func_compare, /*tp_compare*/
  223.     (reprfunc)func_repr, /*tp_repr*/
  224.     0,        /*tp_as_number*/
  225.     0,        /*tp_as_sequence*/
  226.     0,        /*tp_as_mapping*/
  227.     (hashfunc)func_hash, /*tp_hash*/
  228. };
  229.